package com.maizhi.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.maizhi.httpmodel.EasyuiTreeNode;
import com.maizhi.httpmodel.Json;
import com.maizhi.httpmodel.DeptModel;
import com.maizhi.httpmodel.SessionInfo;
import com.maizhi.httpmodel.UserI;
import com.maizhi.model.Dept;
import com.maizhi.model.User;
import com.maizhi.service.BaseServiceI;
import com.maizhi.service.DeptServiceI;
import com.maizhi.service.UserServiceI;
import com.maizhi.util.ResourceUtil;

@Controller
@RequestMapping("/deptController")
public class deptController extends BaseController {

private Logger log = Logger.getLogger(deptController.class);
	
	BaseServiceI<Dept> baseService;
	DeptServiceI deptService;
 
	public BaseServiceI<Dept> getBaseService() {
		return baseService;
	}

	@Autowired
	public void setBaseService(BaseServiceI<Dept> baseService) {
		this.baseService = baseService;
	}

	public DeptServiceI getMenuService() {
		return deptService;
	}

	@Autowired
	public void setMenuService(DeptServiceI deptService) {
		this.deptService = deptService;
	}
 
	//登录用户
	public UserI getLoginUser(HttpSession session){
		SessionInfo sessionInfo;
		try{
			sessionInfo = (SessionInfo)session.getAttribute(ResourceUtil.getSessionInfoName());
			if(sessionInfo==null){
				return null;
			}
		}catch(Exception e){
			return null;
		}
		return sessionInfo.getUser();
	}
	
	/***
	 * 部门信息处理模块*******************************************************
	 */	
	
//部门树 ligerUI
//	@RequestMapping(params = "tree")
//	@ResponseBody
//	public String tree() {
//		List<Dept> list = baseService.find("FROM Dept t");
//		return JSON.toJSONString(list);
//	}
	
	//部门treeGrid
	@RequestMapping(params = "treeGrid")
	@ResponseBody
	public List<DeptModel> treeGrid(String id) {
		log.debug("treeGrid id para : "+id);
		return deptService.treeGrid(id);
	}
	
   //部门管理主页
	@RequestMapping(params = "dept")
	public String dept(){
		return "/xtgl/dept_manger";
	}
	
//	部门管理主页
	@RequestMapping(params = "deptTree")
	public String deptTree(){
		return "/xtgl/dept_tree";
	}
	
	/**
	 * 获取部门树
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(params = "tree")
	@ResponseBody
	public List<EasyuiTreeNode> tree(String id) {
		return deptService.tree(id);
	}
	
	/**
	 * 添加部门
	 * 
	 * @param menu
	 * @return
	 */
	@RequestMapping(params = "add")
	@ResponseBody
	public Json add(DeptModel menu) {
		Json j = new Json();
		DeptModel m = deptService.add(menu);
		j.setSuccess(true);
		j.setMsg("添加成功!");
		return j;
	}

	/**
	 * 删除部门
	 * 
	 * @param Dept
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public Json del(DeptModel menu) {
		Json j = new Json();
		deptService.del(menu);
		j.setSuccess(true);
		j.setMsg("删除成功!");
		return j;
	}

	/**
	 * 编辑部门
	 * 
	 * @param menu
	 * @return
	 */
	@RequestMapping(params = "edit")
	@ResponseBody
	public Json edit(DeptModel menu) {
		Json j = new Json();
		DeptModel m = deptService.edit(menu);
		j.setSuccess(true);
		j.setMsg("编辑成功!");
		return j;
	}

}
